﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using System.Xml.Linq;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Vimeo
{
    public class VimeoService
    {
        private VimeoConfig _config;

        public VimeoService(string config)
        {
            _config = config.DeserializeJsonTo<VimeoConfig>();
        }

        public IList<VimeoGroup> GetGroups()
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/"+ _config.Username.ToLower() +"/groups.xml");

            return (from g in feed.Descendants("group")
                    select g.ToVimeoGroup()).ToList();
        }

        public IList<VimeoVideo> GetGroupVideos(int groupId)
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/group/"+ groupId +"/videos.xml");

            return (from v in feed.Descendants("video")
                    select v.ToVimeoVideo()).ToList();
        }

        public IList<VimeoAlbum> GetAlbums()
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/" + _config.Username.ToLower() + "/albums.xml");

            return (from a in feed.Descendants("album")
                    select a.ToVimeoAlbum()).ToList();
        }

        public IList<VimeoVideo> GetAlbumVideos(int albumId)
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/album/" + albumId + "/videos.xml");

            return (from v in feed.Descendants("video")
                    select v.ToVimeoVideo()).ToList();
        }

        public IList<VimeoChannel> GetChannels()
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/" + _config.Username.ToLower() + "/channels.xml");

            return (from c in feed.Descendants("channel")
                    select c.ToVimeoChannel()).ToList();
        }

        public IList<VimeoVideo> GetChannelVideos(int channelId)
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/channel/" + channelId + "/videos.xml");

            return (from v in feed.Descendants("video")
                    select v.ToVimeoVideo()).ToList();
        }

        public IList<VimeoVideo> GetVideos()
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/" + _config.Username.ToLower() + "/videos.xml");

            return (from v in feed.Descendants("video")
                    select v.ToVimeoVideo()).ToList();
        }

        public VimeoVideo GetVideo(int id)
        {
            var feed = XDocument.Load("http://vimeo.com/api/v2/video/"+ id +".xml");

            return (from v in feed.Descendants("video")
                    select v.ToVimeoVideo()).First();
        }

        public string GetVideoEmbedCode(int id)
        {
            return GetVideoEmbedCode(id, 640, -1, 400, -1, true, true, true, null, false, true, "opaque", true);
        }

        public string GetVideoEmbedCode(int id, 
            int width, 
            int maxWidth, 
            int height, 
            int maxHeight, 
            bool byline, 
            bool title, 
            bool portrait,
            string color, 
            bool autoPlay, 
            bool xhtml, 
            string wmode, 
            bool iframe)
        {
            var url = "http://vimeo.com/api/oembed.xml?url=http%3A//vimeo.com/" + id;

            if (width > 0) url += "&width=" + width;
            if (maxWidth > 0) url += "&maxwidth=" + maxWidth;
            if (height > 0) url += "&height=" + height;
            if (maxHeight > 0) url += "&maxheight=" + maxHeight;
            if (!byline) url += "&byline=0";
            if (!title) url += "&title=0";
            if (!portrait) url += "&portrait=0";
            if (!string.IsNullOrEmpty(color)) url += "&color=" + color;
            if (autoPlay) url += "&autoPlay=1";
            if (!xhtml) url += "&xhtml=0";
            if (!string.IsNullOrEmpty(wmode)) url += "&wmode=" + wmode;
            if (!iframe) url += "&iframe=0";

            var feed = XDocument.Load(url);
            return feed.Root.Element("html").Value;
        }
    }

    #region Model

    public class VimeoVideo
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string ThumbnailSmall { get; set; }
        public string ThumbnailMedium { get; set; }
        public string ThumbnailLarge { get; set; }
        public int Duration { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string Tags { get; set; }
        public DateTime UploadDate { get; set; }
        public string UserName { get; set; }
        public string UserUrl { get; set; }
        public string UserPortraitSmall { get; set; }
        public string UserPortraitMedium { get; set; }
        public string UserPortraitLarge { get; set; }
        public int StatsNumberOfLikes { get; set; }
        public int StatsNumberOfPlays { get; set; }
        public int StatsNumberOfComments { get; set; }
    }

    public class VimeoGroup
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Logo { get; set; }
        public string Thumbnail { get; set; }
        public DateTime CreatedOn { get; set; }
        public int CreatorId { get; set; }
        public string CreatorDisplayName { get; set; }
        public string CreatorUrl { get; set; }
        public int TotalMembers { get; set; }
        public int TotalVideos { get; set; }
        public int TotalFiles { get; set; }
        public int TotalForumTopics { get; set; }
        public int TotalEvents { get; set; }
        public int TotalUpcomingEvents { get; set; }
    }

    public class VimeoAlbum
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string ThumbnailSmall { get; set; }
        public string ThumbnailMedium { get; set; }
        public string ThumbnailLarge { get; set; }
        public DateTime CreatedOn { get; set; }
        public DateTime LastModifiedOn { get; set; }
        public int TotalVideos { get; set; }
    }

    public class VimeoChannel
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Logo { get; set; }
        public string Rss { get; set; }
        public DateTime CreatedOn { get; set; }
        public int CreatorId { get; set; }
        public string CreatorDisplayName { get; set; }
        public string CreatorUrl { get; set; }
        public int TotalMembers { get; set; }
        public int TotalVideos { get; set; }
        public int TotalSubscribers { get; set; }
    }

    #endregion
}